/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.parser.mp.mpost;

import LangTools.LangIdentifyTool;
import cn.ac.iie.di.ban.ycl.idgen.IDGeneratorInterface;
import cn.ac.iie.di.ban.ycl.idgen.IDSHAGeneratorImpl;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.parser.mp.entity.MPostEntity;
import static cn.ac.iie.di.parser.mp.main.ParserMpMain.requestServerSize;
import cn.ac.iie.di.parser.mp.mpost.MPostHandler.SessionPool;
import cn.ac.iie.di.parser.mp.utils.ParserJson;
import cn.ac.iie.di.parser.mp.utils.TransformUtil;
import static cn.ac.iie.di.parser.mp.utils.TransformUtil.strTime2Long;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class MPostThreadPoolTask implements Runnable {

    static private final Logger logger = Logger.getLogger(MPostThreadPoolTask.class.getName());
    private final List<String> jsonList;
    private final int timeout;
    private final SessionPool pool;
    private long starttime = System.currentTimeMillis() / 1000;

    public MPostThreadPoolTask(List<String> jsonList, SessionPool pool, int timeout) {
        this.jsonList = jsonList;
        this.pool = pool;
        this.timeout = timeout;
    }

    @Override
    public void run() {
        RESendSession session = null;
        //计数
        long count = 0;
        //请求去重服务字符串
        String requestString = "";
        //存放mpost对象
        List<MPostEntity> mpostEntityList = new ArrayList<MPostEntity>();

        try {
            session = pool.borrowSession();
            for (String json : jsonList) {
                if (json != null && !json.isEmpty()) {
                    //解析json
                    List<Map<String, String>> dataMapList = new ArrayList<Map<String, String>>();
                    try {
                        //record_listjson数组转map
                        dataMapList = ParserJson.getContentByRecord_list(json);
                        logger.debug("parser json data successfully.");
                    } catch (Exception e) {
                        logger.error("@@@ERROR_MESSAGE@@@:" + json);
                        logger.error("parser data error,cause " + e.getMessage(), e);
                        return;
                    }
//                    logger.info("the json has " + dataMapList.size() + " data.");
                    for (int i = 0; i < dataMapList.size(); i++) {
                        MPostEntity mpostEntity = new MPostEntity();
                        Map<String, String> dataMap = new HashMap<String, String>();
                        dataMap = dataMapList.get(i);

                        IDGeneratorInterface idge = new IDSHAGeneratorImpl();

                        //待定字段名称dataMap为单个json
                        String g_ch_key = dataMap.get("content_id");
                        mpostEntity.setG_ch_key(g_ch_key);

                        String m_parent_id = dataMap.get("parent_id");
                        mpostEntity.setM_parent_id(m_parent_id);
                        //文章url可为空
                        String m_url = dataMap.get("article_url");
                        if (m_url == null || m_url.isEmpty() || "null".equals(m_url)) {
//                            logger.info("article_url = null");
                            mpostEntity.setM_url(m_url);
                        } else {
                            mpostEntity.setM_url(m_url);
                        }

                        String m_cont_type = dataMap.get("content_type");
                        mpostEntity.setM_cont_type(m_cont_type);

                        String u_ch_id = dataMap.get("author_id");
                        mpostEntity.setU_ch_id(u_ch_id);

                        String g_asp = dataMap.get("from");
                        mpostEntity.setG_asp(g_asp);

                        String u_name = dataMap.get("author_name");
                        if (u_name == null || u_name.isEmpty() || "null".equals(u_name)) {
//                            logger.info("u_name = null");
                            mpostEntity.setU_name(u_name);
                        } else {
                            mpostEntity.setU_name(u_name);
                        }

                        String m_title = dataMap.get("title");
                        mpostEntity.setM_title(m_title);
                        //文章字数从json中提取的时候应该是int
                        String m_content_len = dataMap.get("text_length");
                        mpostEntity.setM_content_len(Integer.parseInt(m_content_len));

                        String ma_picture_cnt = dataMap.get("image_num");
                        mpostEntity.setMa_picture_cnt(Integer.parseInt(ma_picture_cnt));

                        String ma_video_cnt = dataMap.get("video_num");
                        mpostEntity.setMa_video_cnt(Integer.parseInt(ma_video_cnt));
                        //发表时间yyyy-MM-dd HH:mm:ss格式m_publish_time未推送，则填当前时间
                        //M_pt_is_generated如果create_time有值则填0；无值则填1"

                        String m_publish_time = dataMap.get("create_time");
                        if (m_publish_time == null || m_publish_time.isEmpty() || "null".equals(m_publish_time)) {
                            long m_publish_time_stamp = System.currentTimeMillis() / 1000;
                            mpostEntity.setM_publish_time(m_publish_time_stamp);
                            mpostEntity.setM_pt_is_generated(1);
                        } else {
                            mpostEntity.setM_publish_time(TransformUtil.stringTime2Long(m_publish_time));
                            mpostEntity.setM_pt_is_generated(0);
                        }

                        //默认值为null如何填
                        String m_abstract = dataMap.get("abstract");
                        if (m_abstract == null || m_abstract.isEmpty() || "null".equals(m_abstract)) {
//                            logger.info("m_abstract = null");
                            mpostEntity.setM_abstract(m_abstract);
                        } else {
                            mpostEntity.setM_abstract(m_abstract);
                        }

                        String m_content = dataMap.get("content");
                        mpostEntity.setM_content(m_content);

                        String m_classStr = dataMap.get("classes");//应该存一个数组                        
                        if (m_classStr == null || m_classStr.isEmpty() || "null".equals(m_classStr) || m_classStr.equals("[]")) {
//                            logger.info("m_classStr is null");
                            mpostEntity.setM_class(TransformUtil.string2Strings(m_classStr));
                        } else {
                            mpostEntity.setM_class(TransformUtil.string2Strings(m_classStr));
                        }

                        String m_snapshot_time = dataMap.get("snapshot_time");
                        mpostEntity.setM_snapshot_time(TransformUtil.stringTime2Long(m_snapshot_time));

                        String m_update_time = dataMap.get("modify_time");
                        mpostEntity.setM_update_time(TransformUtil.stringTime2Long(m_update_time));
                        //stirngs是什么样的，例子
                        String m_post_type = dataMap.get("post_type");
                        if (m_post_type == null || m_post_type.isEmpty() || "null".equals(m_post_type) || m_post_type.equals("[]")) {
//                            logger.info("m_classStr is null");
                            mpostEntity.setM_post_type(TransformUtil.string2Strings(m_post_type));
                        } else {
                            mpostEntity.setM_post_type(TransformUtil.string2Strings(m_post_type));
                        }
                        //long的默认值是什么
                        String mo_status = dataMap.get("is_del");
                        if (mo_status == null || mo_status.equals("") || "null".equals(mo_status)) {
                            mpostEntity.setMo_status(-1);
                        } else {
                            //帖子是否被删除，删除填0，否则填1
                            if (mo_status.equals("0")) {
                                mpostEntity.setMo_status(0);
                            } else {
                                mpostEntity.setMo_status(1);
                            }
                        }
                        //帖子的删除时间yyyyMMddHHmmss格式,long时间戳默认值是什么
                        String mo_time = dataMap.get("del_time");
                        if (mo_time == null || mo_time.equals("") || "null".equals(mo_time)) {
//                            logger.info("mo_time is null");
                            mpostEntity.setMo_time(-1);
                        } else {
                            mpostEntity.setMo_time(strTime2Long(mo_time));
                        }

                        String m_like_cnt = dataMap.get("like_num");
                        if (m_like_cnt == null || m_like_cnt.equals("") || "null".equals(m_like_cnt)) {
//                            logger.info("m_like_cnt is null");
                            mpostEntity.setM_like_cnt(-1);
                        } else {
                            mpostEntity.setM_like_cnt(Long.parseLong(m_like_cnt));
                        }

                        String m_read_cnt = dataMap.get("view_num");
                        if (m_read_cnt == null || m_read_cnt.equals("") || "null".equals(m_read_cnt)) {
//                            logger.info("m_read_cnt is null");
                            mpostEntity.setM_read_cnt(-1);
                        } else {
                            mpostEntity.setM_read_cnt(Long.parseLong(m_read_cnt));
                        }

                        String m_reply_cnt = dataMap.get("comment_num");
                        if (m_reply_cnt == null || m_reply_cnt.equals("") || "null".equals(m_reply_cnt)) {
//                            logger.info("m_reply_cnt is null");
                            mpostEntity.setM_reply_cnt(-1);
                        } else {
                            mpostEntity.setM_reply_cnt(Long.parseLong(m_reply_cnt));
                        }

                        String m_html_content = dataMap.get("text");
                        if (m_html_content == null || m_html_content.equals("") || "null".equals(m_html_content)) {
//                            logger.info("m_reply_cnt is null");
                            mpostEntity.setM_html_content(m_html_content);
                        } else {
                            mpostEntity.setM_html_content(m_html_content);
                        }

                        String ma_picture_urls = dataMap.get("img_url");
                        if (ma_picture_urls == null || ma_picture_urls.isEmpty() || "null".equals(ma_picture_urls) || ma_picture_urls.equals("[]")) {
//                            logger.info("ma_picture_urls is null");
                            mpostEntity.setMa_picture_urls(TransformUtil.string2Strings(ma_picture_urls));
                        } else {
                            mpostEntity.setMa_picture_urls(TransformUtil.string2Strings(ma_picture_urls));
                        }
                        //img_width": [1604,],ints为空如何填
                        String ma_picture_width = dataMap.get("img_width");
                        int[] ma_picture_width_intArray = TransformUtil.stringToInts(ma_picture_width);
                        mpostEntity.setMa_picture_width(ma_picture_width_intArray);

                        String ma_picture_height = dataMap.get("img_height");
                        int[] ma_picture_height_intArray = TransformUtil.stringToInts(ma_picture_height);
                        mpostEntity.setMa_picture_height(ma_picture_height_intArray);

                        String ma_video_urls = dataMap.get("video_url");
                        if (ma_video_urls == null || ma_video_urls.isEmpty() || "null".equals(ma_video_urls) || ma_video_urls.equals("[]")) {
//                            logger.info("ma_video_urls is null");
                            mpostEntity.setMa_video_urls(TransformUtil.string2Strings(ma_video_urls));
                        } else {
                            mpostEntity.setMa_video_urls(TransformUtil.string2Strings(ma_video_urls));
                        }

                        String ma_video_urls_cover = dataMap.get("video_thumbnail");
                        mpostEntity.setMa_video_urls_cover(TransformUtil.string2Strings(ma_video_urls_cover));

                        String m_hyperlink = dataMap.get("link");
                        mpostEntity.setM_hyperlink(TransformUtil.string2Strings(m_hyperlink));

                        String m_hyperlink_desc = dataMap.get("link_desc");
                        mpostEntity.setM_hyperlink_desc(TransformUtil.string2Strings(m_hyperlink_desc));

                        try {
                            mpostEntity.setM_year((int) Long.parseLong(m_publish_time.split(" ")[0].split("-")[0]));
                            mpostEntity.setM_month((int) Long.parseLong(m_publish_time.split(" ")[0].split("-")[1]));
                            mpostEntity.setM_day((int) Long.parseLong(m_publish_time.split(" ")[0].split("-")[2]));
                            mpostEntity.setM_hour((int) Long.parseLong(m_publish_time.split(" ")[1].split(":")[0]));
                        } catch (Exception e) {
                            logger.error("(m_publish_time)Type conversion error,cause " + e.getMessage(), e);
                            logger.warn("@@m_publish_time:" + m_publish_time);
                            mpostEntity.setM_year(-1);
                            mpostEntity.setM_month(-1);
                            mpostEntity.setM_day(-1);
                            mpostEntity.setM_hour(-1);
                        }

                        mpostEntity.setM_insert_time(System.currentTimeMillis() / 1000);

                        String langType = "";
                        try {
                            LangIdentifyTool langtool = new LangIdentifyTool();
                            langtool.initText(mpostEntity.getM_html_content());
                            langType = langtool.getLangType();
                            mpostEntity.setM_language(langType);
                        } catch (Exception e) {
                            logger.error("(m_language)get language error,cause " + e.getMessage(), e);
                            logger.warn("@@m_content:" + mpostEntity.getM_content());
                            logger.warn("the language return " + langType);
                            mpostEntity.setM_language("");
                        }
                        count++;
                        //去重服务,去重服务如何使用，待定
                        mpostEntityList.add(mpostEntity);

                        //请求去重服务并发送数据
                        if (count % requestServerSize == 0 && count != 0) {
                            sendMessageToMQ(mpostEntityList, session);
                            mpostEntityList.clear();
                        }
                    }//一条json数据
                }
            }//所有json数据,发送消息队列
            if (!mpostEntityList.isEmpty()) {
                sendMessageToMQ(mpostEntityList, session);
                count = 0;
                requestString = "";
                mpostEntityList.clear();
            }
//            logger.info("A total " + count + " data of the mpost.");
        } catch (Exception ex) {
            logger.error("setSession error, cause " + ex.getMessage(), ex);
        } finally {
            if (session != null) {
                pool.returnSession(session);
            }
        }
    }
//明天完善去重服务和发送mq方法重组

    public void sendMessageToMQ(List<MPostEntity> mpostEntityList, RESendSession session) {
        //循环发送数据
        try {
            //将realList更换成mpostEntityList了，这样的数据就不是去重之后的数据了，到时候更改回来即可
            //20170426 已经修改回来了
            for (int b = 0; b < mpostEntityList.size(); b++) {

                MPostEntity entity = mpostEntityList.get(b);
                if (entity != null) {
                    try {
                        session.setString("g_ch_key", entity.getG_ch_key());
                        session.setString("m_parent_id", entity.getM_parent_id());
                        session.setString("m_url", entity.getM_url());
                        session.setString("m_cont_type", entity.getM_cont_type());
                        session.setString("u_ch_id", entity.getU_ch_id());
                        session.setString("g_asp", entity.getG_asp());
                        session.setString("u_name", entity.getU_name());
                        session.setString("m_title", entity.getM_title());
                        session.setInt("m_content_len", entity.getM_content_len());
                        session.setInt("ma_picture_cnt", entity.getMa_picture_cnt());
                        session.setInt("ma_video_cnt", entity.getMa_video_cnt());
                        session.setLong("m_publish_time", entity.getM_publish_time());
                        session.setInt("m_pt_is_generated", entity.getM_pt_is_generated());
                        session.setString("m_abstract", entity.getM_abstract());
                        session.setString("m_content", entity.getM_content());
                        session.setStrings("m_class", entity.getM_class());
                        session.setLong("m_snapshot_time", entity.getM_snapshot_time());
                        session.setLong("m_update_time", entity.getM_update_time());
                        session.setStrings("m_post_type", entity.getM_post_type());
                        session.setLong("mo_status", entity.getMo_status());
                        session.setLong("m_like_cnt", entity.getM_like_cnt());
                        session.setLong("m_read_cnt", entity.getM_read_cnt());
                        session.setLong("m_reply_cnt", entity.getM_reply_cnt());
                        session.setString("m_html_content", entity.getM_html_content());
                        session.setStrings("ma_picture_urls", entity.getMa_picture_urls());
                        session.setInts("ma_picture_width", entity.getMa_picture_width());
                        session.setInts("ma_picture_height", entity.getMa_picture_height());
                        session.setStrings("ma_video_urls", entity.getMa_video_urls());
                        session.setStrings("ma_video_urls_cover", entity.getMa_video_urls_cover());
                        session.setStrings("m_hyperlink", entity.getM_hyperlink());
                        session.setStrings("m_hyperlink_desc", entity.getM_hyperlink_desc());
                        session.setInt("m_year", entity.getM_year());
                        session.setInt("m_month", entity.getM_month());
                        session.setInt("m_day", entity.getM_day());
                        session.setInt("m_hour", entity.getM_hour());
                        session.setLong("m_insert_time", entity.getM_insert_time());
                        session.setString("m_language", entity.getM_language());

                        session.add();
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
//                        session.add();
//                        sendMSG(session);

                    }

                    if (System.currentTimeMillis() / 1000 - starttime > timeout) {
                        starttime = System.currentTimeMillis() / 1000;
                        sendMSG(session);
                        logger.info("timeout.");
                    }
                }
            }
            sendMSG(session);
            logger.info("mpostEntityList " + mpostEntityList.size() + ",send datas of mpost: " + mpostEntityList.size());
        } catch (Exception ex) {
            logger.error("setSession error, cause " + ex.getMessage(), ex);
        }
    }

    public void sendMSG(RESendSession session) {
        while (true) {
            try {
                logger.info("flush...");
                session.flush();
                break;
            } catch (Exception ex) {
                logger.info("what's up buddy ? how's it going ?" + ex, ex);
            }
        }
    }

    public String maps2json(Map[] maps) {
        String jsons = "";
        for (Map map : maps) {
            String json = JSONObject.toJSONString(map);
            jsons += (json + ",");
        }
        if (maps.length > 0) {
            jsons = jsons.substring(0, jsons.lastIndexOf(","));
        }
        jsons = "[" + jsons + "]";
        return jsons;
    }
}
